/* p11_1.c: Reading the PWM Counter */

/* This program sets up the counter in PWM1 Generator 3 for down counter and reload with maximum value. The counter value is continuously read and the three most significant bits are written to the LEDs. */

#include "TM4C123GH6PM.h"

int main(void)
{
    int x;

    /* Enable Peripheral Clocks */
    SYSCTL->RCGCPWM |= 2;       /* enable clock to PWM1 */
    SYSCTL->RCGCGPIO |= 0x20;   /* enable clock to PORTF */
    SYSCTL->RCC |= 0x00100000;  /* use pre-divide for PWM clock */
    SYSCTL->RCC |= 0x000E0000;  /* use 64 for pre-divide for PWM */

    /* Enable PORTF3-1 for tri-color LEDs */
    GPIOF->PCTL &= ~0x0000FFF0; /* make PORTF2 PWM output pin */
    GPIOF->DIR |= 0x0E;          /* pins output */
    GPIOF->DEN |= 0x0E;          /* pins digital */
    
    /* set up counter in PWM1 Generator 3 */
    PWM1->_3_CTL = 0;           /* stop counter */
    PWM1->_3_LOAD = 0X0000FFFF; /* set max load value */
    PWM1->_3_CTL = 1;           /* start counter */
    	
    for(;;)
    {
        x = PWM1->_3_COUNT;     /* read counter value */
        x = x >> 12;            /* shift it right 12 bits */
        GPIOF->DATA = x;        /* write it to the LEDs */
    }
}

/* This function is called by the startup assembly code to perform system specific initialization tasks. */
void SystemInit(void)
{
    /* Grant coprocessor access */
    /* This is required since TM4C123G has a floating point coprocessor */
    SCB->CPACR |= 0x00f00000;
}